package com.qo.qsdk.ui.base;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.qo.qsdk.model.HisModel;
import com.qo.qsdk.ui.QSDKListener;
import com.qo.qsdk.utils.DynamicUtils;
import com.qo.qsdk.utils.Language;

import java.util.ArrayList;
import java.util.List;

public class HisView implements IBaseView{

    private Activity mActivity;
    private QRecyclerView mRecyclerView;
    private TextView mHisTitle;
    private HisAdapter mAdapter;
    private View mMain;
    private List<HisModel> list = new ArrayList<>();
    private QSDKListener mQSDKListener;

    public HisView(Activity mActivity, QSDKListener qsdkListener) {
        this.mActivity = mActivity;
        this.mQSDKListener = qsdkListener;
        init(mActivity);
    }

    public void addData(List<HisModel> list) {
        this.list.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    private void init(Activity v) {
        mMain = v.findViewById(DynamicUtils.id(mActivity, "mainHis"));
        mMain.setVisibility(View.GONE);
        mAdapter = new HisAdapter(v, list);
        mAdapter.setHisAdapterLister(new HisAdapter.HisAdapterLister() {
            @Override
            public void onItemClick(int pos) {
                HisModel model = list.get(pos);
                if (model.getItemType() == HisModel.ITEM_TYPE.ITEM_TYPE_SECTION && mQSDKListener != null) {
                    switch (model.getType()) {
                        case Google:
                            mQSDKListener.onHisGoogle(model.getAccount());
                            break;
                        case Apple:
                            mQSDKListener.onHisApple(model.getAccount());
                            break;
                        case Guest:
                            mQSDKListener.onHisGuest(model.getAccount());
                            break;
                        case Account:
                            mQSDKListener.onHisAccount(model.getAccount());
                            break;
                        case Facebook:
                            mQSDKListener.onHisFacebook(model.getAccount());
                            break;
                    }
                }
            }
        });
        mRecyclerView = v.findViewById(DynamicUtils.id(mActivity, "hisRecyclerView"));
        mHisTitle = v.findViewById(DynamicUtils.id(mActivity, "hisTitle"));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(v));
        v.findViewById(DynamicUtils.id(mActivity, "closeHis")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mQSDKListener != null) mQSDKListener.onHisClose();
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        refresh();
    }

    @Override
    public void show(Activity activity) {
        mMain.setVisibility(View.VISIBLE);
    }

    @Override
    public void hide(Activity activity) {
        mMain.setVisibility(View.GONE);
    }

    @Override
    public void refresh() {
        mHisTitle.setText(Language.His_Tip.toString());
        if (mAdapter != null) mAdapter.notifyDataSetChanged();
    }
}
